Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support history_file configuration per dsn, and --history option. #1216

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

laixintao
Copy link
Member

@laixintao laixintao commented Oct 15, 2020

Description

This feature enables the user to config history file location for specific dsn.

close: #535

TODOs

  • --hisfile support
  • history file per database
    • switch history file when switch database
  • alias dsn histfile

Checklist

  • I've added this contribution to the changelog.rst.
  • I've added my name to the AUTHORS file (or it's already there).
  • I installed pre-commit hooks (pip install pre-commit && pre-commit install), and ran black on my code.
  • Please squash merge this pull request (uncheck if you'd like us to merge as multiple commits)

Copy link
Contributor

@j-bennet j-bennet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some comments for you.

pgcli/main.py Outdated
@@ -1194,6 +1194,9 @@ def echo_via_pager(self, text, color=None):
@click.option(
"--warn/--no-warn", default=None, help="Warn before running a destructive query."
)
@click.option(
"--history", default=None, help="Specify history file location."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps histfile, so it matches the name used by psql?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent! I knew I met --histfile somewhere but I forgot it's psql, I just haven't use psql for a long time. :)

@@ -187,6 +188,11 @@ output.null = "#808080"
[alias_dsn]
# example_dsn = postgresql://[user[:password]@][netloc][:port][/dbname]

# Specify sperate history file for alias_dsn.
# If not set, will use history_file settings.
[dsn_history_file]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can support mapping of history file to not only DSN aliases, but database names as well. That way, if you have mydb1 hosted on different servers, you could just configure:

[db_history_file]
mydb1 = ~/config/pgcli/history_mydb1

this could be useful to users that do not use DSN aliases.

There will be ambiguity if we do that: we'd have to figure out if mydb1 is a DSN alias or a database name. We can look for matching DSN alias first, if user provided --dsn when running the cli, and if there's no alias found, assume this is database name.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I like the idea that mapping of the history file for the database. I think it covers most of the use cases. Maybe consider alias_dsn is kind of overthinking and over-designed, I think we add too mush to configuration, and cloud leads to misunderstanding. I don't think users want to share history between databases. Even if they want to do that, they can use --histfile option.

Let's see, we only add a flag sperate_history_on_database_name = False, and add --histfile option. (Also, if user use \c[onnect] database_name, we should switch history file as well.

If the user does want to share history between database, they can:

  1. Use --histfile, maybe use shell's alias together.
  2. Save the most used query to named query.

What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that:

  • separating history per database does not need a config flag. Once that feature is in, we should do it by default. When you're connected to a specific database, queries from other databases are not very useful / relevant, and would only clutter history.
  • user can use --histfile to override the default history-file-per-database.
  • mapping to DSN vs database name can be useful. I had a use case with prod and beta database tables named table1_prod, table2_prod etc. Table structure was the same, but table names were different, so you could not use exactly the same query on those databases. This may be an edge case.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am convinced, I will only add a section named dsn_history_file section(maybe we cloud come up with a better name), and left other configs untouched.

So the process of choosing a history file will look like this:

image

Do I understand it correctly?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this looks exactly right.

I added two args for PGCli init, because I think we need to figure out
which history file to use on `__init__`. And we will need `histfile`
`alias_dsn` for that.

PS: I don't think change PGCli's property after initializing is good.
@codecov-io
Copy link

codecov-io commented Dec 2, 2020

Codecov Report

Merging #1216 (2588068) into master (7626d9a) will decrease coverage by 0.01%.
The diff coverage is 77.77%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1216      +/-   ##
==========================================
- Coverage   83.78%   83.76%   -0.02%     
==========================================
  Files          21       21              
  Lines        2559     2563       +4     
==========================================
+ Hits         2144     2147       +3     
- Misses        415      416       +1     
Impacted Files Coverage Δ
pgcli/main.py 75.63% <77.77%> (-0.01%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7626d9a...2588068. Read the comment docs.

@gdevanla
Copy link

I like this feature and would love this PR to come through. @laixintao are you still working on this. Is there something I can do to help?

@laixintao
Copy link
Member Author

laixintao commented Jan 13, 2021 via email

@laixintao
Copy link
Member Author

I am so sorry, totally forgot this PR. I will get back to this

@saucoide
Copy link
Contributor

@laixintao i'd like to help finish this one, do you mind if i try to pick it up?

@laixintao
Copy link
Member Author

@laixintao i'd like to help finish this one, do you mind if i try to pick it up?

sure, thanks! (shame on me 🥺)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Command history configuration/HISTFILE option
5 participants